﻿using System;
using FirebirdSql.Data.FirebirdClient;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Data;

namespace RMSM
{
    [Serializable()]
    public class TrangPhucBO : Csla.BusinessBase<TrangPhucBO>
    {
        #region Business Properties
        //TRANGPHUCID
        private int? _TrangPhucID = null;
        public int? TrangPhucID
        {
            get { return this._TrangPhucID; }
            set { this._TrangPhucID = value; PropertyHasChanged("TrangPhucID"); }
        }

        //LOAITRANGPHUCID
        private int? _LoaiTrangPhucID = Session.LoaiTrangPhucID;
        public int? LoaiTrangPhucID
        {
            get { return this._LoaiTrangPhucID; }
            set { this._LoaiTrangPhucID = value; PropertyHasChanged("LoaiTrangPhucID"); }
        }

        //MA
        private string _Ma = null;
        public string Ma
        {
            get { return this._Ma; }
            set { this._Ma = value; PropertyHasChanged("Ma"); }
        }

        //TEN
        private string _Ten = null;
        public string Ten
        {
            get { return this._Ten; }
            set { this._Ten = value; PropertyHasChanged("Ten"); }
        }

        //MAUSAC
        private string _MauSac = null;
        public string MauSac
        {
            get { return this._MauSac; }
            set
            {
                this._MauSac = value;

                if (this._MauSac != null)
                {
                    string sTmp = this._MauSac.ToUpper();
                    if (Session.hstMauSac.ContainsKey(sTmp) == false)
                        Session.hstMauSac.Add(sTmp, this._MauSac);
                }

                PropertyHasChanged("MauSac");
            }
        }

        //MAMAUSAC
        private int? _MaMauSac = null;
        public int? MaMauSac
        {
            get { return this._MaMauSac; }
            set { this._MaMauSac = value; PropertyHasChanged("MaMauSac"); }
        }

        //ANH
        private byte[] _Anh = null;
        public byte[] Anh
        {
            get { return this._Anh; }
            set { this._Anh = value; PropertyHasChanged("Anh"); }
        }

        //XUATXU
        private string _XuatXu = null;
        public string XuatXu
        {
            get { return this._XuatXu; }
            set
            {
                this._XuatXu = value;

                if (this._XuatXu != null)
                {
                    string sTmp = this._XuatXu.ToUpper();
                    if (Session.hstXuatXu.ContainsKey(sTmp) == false)
                        Session.hstXuatXu.Add(sTmp, this._XuatXu);
                }

                PropertyHasChanged("XuatXu");
            }
        }

        //PHUKIEN
        private string _PhuKien = null;
        public string PhuKien
        {
            get { return this._PhuKien; }
            set
            {
                this._PhuKien = value;

                if (this._PhuKien != null)
                {
                    string sTmp = this._PhuKien.ToUpper();
                    if (Session.hstPhuKien.ContainsKey(sTmp) == false)
                        Session.hstPhuKien.Add(sTmp, this._PhuKien);
                }

                PropertyHasChanged("PhuKien");
            }
        }

        //NGAYNHAP
        private DateTime? _NgayNhap = DateTime.Now;
        public DateTime? NgayNhap
        {
            get { return this._NgayNhap; }
            set { this._NgayNhap = value; PropertyHasChanged("NgayNhap"); }
        }

        //GIACHOTHUE
        private double? _GiaChoThue = 0;
        public double? GiaChoThue
        {
            get { return this._GiaChoThue; }
            set { this._GiaChoThue = value; PropertyHasChanged("GiaChoThue"); }
        }

        //GIANHAP
        private double? _GiaNhap = 0;
        public double? GiaNhap
        {
            get { return this._GiaNhap; }
            set { this._GiaNhap = value; PropertyHasChanged("GiaNhap"); }
        }

        //GIABAN
        private double? _GiaBan = 0;
        public double? GiaBan
        {
            get { return this._GiaBan; }
            set { this._GiaBan = value; PropertyHasChanged("GiaBan"); }
        }

        //CONSUDUNG
        private bool? _ConSuDung = true;
        public bool? ConSuDung
        {
            get { return this._ConSuDung; }
            set { this._ConSuDung = value; PropertyHasChanged("ConSuDung"); }
        }

        //LANSUDUNG
        private int? _LanSuDung = 0;
        public int? LanSuDung
        {
            get { return this._LanSuDung; }
            set { this._LanSuDung = value; PropertyHasChanged("LanSuDung"); }
        }

        //DELE
        private bool? _DELE = false;
        public bool? DELE
        {
            get { return this._DELE; }
            set { this._DELE = value; PropertyHasChanged("DELE"); }
        }


        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; }
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        protected override object GetIdValue()
        {
            return _TrangPhucID.Value;
        }

        private static int newTrangPhucID = -1;
        private TrangPhucBO()
        {
            _TrangPhucID = newTrangPhucID--;
        }

        public TrangPhucBO(FbDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public TrangPhucBO(FbDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static TrangPhucBO NewRootTrangPhucBO()
        {
            return DataPortal.Create<TrangPhucBO>(new RootCriteria());
        }

        internal static TrangPhucBO NewChildTrangPhucBO()
        {
            return DataPortal.Create<TrangPhucBO>(new ChildCriteria());
        }

        public static TrangPhucBO GetRootTrangPhucBO(int _TrangPhucID)
        {
            return DataPortal.Fetch<TrangPhucBO>(new RootCriteria(_TrangPhucID));
        }

        public static TrangPhucBO GetRootTrangPhucBOByWhere(string where)
        {
            return DataPortal.Fetch<TrangPhucBO>(where);
        }

        internal static TrangPhucBO GetChildTrangPhucBO(FbDataReader dataReader)
        {
            return new TrangPhucBO(dataReader);
        }

        public static void DeleteTrangPhucBO(int _TrangPhucID)
        {
            DataPortal.Delete(new RootCriteria(_TrangPhucID));
        }

        public static void DeleteTrangPhucBO(int _TrangPhucID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_TrangPhucID, directDelete));
        }

        public static string GetIDTrangPhucBO()
        {
            return "TrangPhucID";
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int TrangPhucID;
            public bool directDelete = false;
            public RootCriteria(int _TrangPhucID)
            {
                this.TrangPhucID = _TrangPhucID;
                this.directDelete = false;
            }

            public RootCriteria(int _TrangPhucID, bool directDelete)
            {
                this.TrangPhucID = _TrangPhucID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int TrangPhucID;
            public bool directDelete = false;
            public ChildCriteria(int _TrangPhucID)
            {
                this.TrangPhucID = _TrangPhucID;
                this.directDelete = false;
            }

            public ChildCriteria(int _TrangPhucID, bool directDelete)
            {
                this.TrangPhucID = _TrangPhucID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
            using (FbConnection conn = new FbConnection(Session.CnnStr))
            {
                conn.Open();
                using (FbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "TrangPhucBO_B";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@WHEREEXP", FbDbType.VarChar).Value = "TRANGPHUCID = " + criteria.TrangPhucID;

                    using (FbDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.Read())
                            Fetch(reader);
                    }
                }
            }
        }

        private void This_Fetch(string where)
        {
            using (FbConnection conn = new FbConnection(Session.CnnStr))
            {
                conn.Open();
                using (FbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "TrangPhucBO_B";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@WHEREEXP", FbDbType.VarChar).Value = where;

                    using (FbDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.Read())
                            Fetch(reader);
                    }
                }
            }
        }

        private void Fetch(FbDataReader dataReader)
        {
            if (!(dataReader["TRANGPHUCID"] is DBNull))
                this._TrangPhucID = (int)dataReader["TRANGPHUCID"];
            if (!(dataReader["LOAITRANGPHUCID"] is DBNull))
                this._LoaiTrangPhucID = (int)dataReader["LOAITRANGPHUCID"];
            if (!(dataReader["MA"] is DBNull))
                this._Ma = ((string)dataReader["MA"]).Trim();
            if (!(dataReader["TEN"] is DBNull))
                this._Ten = ((string)dataReader["TEN"]).Trim();
            if (!(dataReader["MAUSAC"] is DBNull))
                this._MauSac = ((string)dataReader["MAUSAC"]).Trim();
            if (!(dataReader["MAMAUSAC"] is DBNull))
                this._MaMauSac = (int)dataReader["MAMAUSAC"];
            if (!(dataReader["ANH"] is DBNull))
                this._Anh = (byte[])dataReader["ANH"];
            if (!(dataReader["XUATXU"] is DBNull))
                this._XuatXu = ((string)dataReader["XUATXU"]).Trim();
            if (!(dataReader["PHUKIEN"] is DBNull))
                this._PhuKien = ((string)dataReader["PHUKIEN"]).Trim();
            if (!(dataReader["NGAYNHAP"] is DBNull))
                this._NgayNhap = (DateTime)dataReader["NGAYNHAP"];
            if (!(dataReader["GIACHOTHUE"] is DBNull))
                this._GiaChoThue = Convert.ToDouble(dataReader["GIACHOTHUE"]);
            if (!(dataReader["GIANHAP"] is DBNull))
                this._GiaNhap = Convert.ToDouble(dataReader["GIANHAP"]);
            if (!(dataReader["GIABAN"] is DBNull))
                this._GiaBan = Convert.ToDouble(dataReader["GIABAN"]);
            if (!(dataReader["CONSUDUNG"] is DBNull))
                this._ConSuDung = (((string)dataReader["CONSUDUNG"]).Trim() == "1");
            if (!(dataReader["LANSUDUNG"] is DBNull))
                this._LanSuDung = (int)dataReader["LANSUDUNG"];
            if (!(dataReader["DELE"] is DBNull))
                this._DELE = (((string)dataReader["DELE"]).Trim() == "1");

        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
            using (FbConnection conn = new FbConnection(Session.CnnStr))
            {
                conn.Open();
                using (FbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "TrangPhucBO_I";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@LOAITRANGPHUCID", FbDbType.Integer).Value = this._LoaiTrangPhucID;
                    cmd.Parameters.Add("@MA", FbDbType.VarChar).Value = this._Ma;
                    cmd.Parameters.Add("@TEN", FbDbType.VarChar).Value = this._Ten;
                    cmd.Parameters.Add("@MAUSAC", FbDbType.VarChar).Value = this._MauSac;
                    cmd.Parameters.Add("@MAMAUSAC", FbDbType.Integer).Value = this._MaMauSac;
                    cmd.Parameters.Add("@ANH", FbDbType.Binary).Value = this._Anh;
                    cmd.Parameters.Add("@XUATXU", FbDbType.VarChar).Value = this._XuatXu;
                    cmd.Parameters.Add("@PHUKIEN", FbDbType.VarChar).Value = this._PhuKien;
                    cmd.Parameters.Add("@NGAYNHAP", FbDbType.TimeStamp).Value = this._NgayNhap;
                    cmd.Parameters.Add("@GIACHOTHUE", FbDbType.Float).Value = this._GiaChoThue;
                    cmd.Parameters.Add("@GIANHAP", FbDbType.Float).Value = this._GiaNhap;
                    cmd.Parameters.Add("@GIABAN", FbDbType.Float).Value = this._GiaBan;
                    cmd.Parameters.Add("@CONSUDUNG", FbDbType.Char).Value = (this._ConSuDung.Value ? "1" : "0");
                    cmd.Parameters.Add("@LANSUDUNG", FbDbType.Integer).Value = this._LanSuDung;
                    cmd.Parameters.Add("@DELE", FbDbType.Char).Value = (this._DELE.Value ? "1" : "0");

                    FbParameter pTrangPhucID = new FbParameter("@TRANGPHUCID", FbDbType.Integer);
                    pTrangPhucID.Direction = ParameterDirection.ReturnValue;
                    cmd.Parameters.Add(pTrangPhucID);

                    cmd.ExecuteNonQuery();
                    this._TrangPhucID = Convert.ToInt32(cmd.Parameters["@TRANGPHUCID"].Value);
                }
            }
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
            using (FbConnection conn = new FbConnection(Session.CnnStr))
            {
                conn.Open();
                using (FbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "TrangPhucBO_U";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@TRANGPHUCID", FbDbType.Integer).Value = this._TrangPhucID;
                    cmd.Parameters.Add("@LOAITRANGPHUCID", FbDbType.Integer).Value = this._LoaiTrangPhucID;
                    cmd.Parameters.Add("@MA", FbDbType.VarChar).Value = this._Ma;
                    cmd.Parameters.Add("@TEN", FbDbType.VarChar).Value = this._Ten;
                    cmd.Parameters.Add("@MAUSAC", FbDbType.VarChar).Value = this._MauSac;
                    cmd.Parameters.Add("@MAMAUSAC", FbDbType.Integer).Value = this._MaMauSac;
                    cmd.Parameters.Add("@ANH", FbDbType.Binary).Value = this._Anh;
                    cmd.Parameters.Add("@XUATXU", FbDbType.VarChar).Value = this._XuatXu;
                    cmd.Parameters.Add("@PHUKIEN", FbDbType.VarChar).Value = this._PhuKien;
                    cmd.Parameters.Add("@NGAYNHAP", FbDbType.TimeStamp).Value = this._NgayNhap;
                    cmd.Parameters.Add("@GIACHOTHUE", FbDbType.Float).Value = this._GiaChoThue;
                    cmd.Parameters.Add("@GIANHAP", FbDbType.Float).Value = this._GiaNhap;
                    cmd.Parameters.Add("@GIABAN", FbDbType.Float).Value = this._GiaBan;
                    cmd.Parameters.Add("@CONSUDUNG", FbDbType.Char).Value = (this._ConSuDung.Value ? "1" : "0");
                    cmd.Parameters.Add("@LANSUDUNG", FbDbType.Integer).Value = this._LanSuDung;
                    cmd.Parameters.Add("@DELE", FbDbType.Char).Value = (this._DELE.Value ? "1" : "0");

                    cmd.ExecuteNonQuery();
                }
            }
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_TrangPhucID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
            using (FbConnection conn = new FbConnection(Session.CnnStr))
            {
                conn.Open();
                using (FbCommand cmd = conn.CreateCommand())
                {
                    if (criteria.directDelete)
                    {
                        cmd.CommandText = "DELETE FROM TRANGPHUC WHERE TRANGPHUCID = " + criteria.TrangPhucID + ";";
                        cmd.ExecuteNonQuery();
                    }
                    else
                    {
                        cmd.CommandText = "UPDATE TRANGPHUC SET TRANGPHUC.DELE = '1' WHERE TRANGPHUCID = " + criteria.TrangPhucID + ";";
                        cmd.ExecuteNonQuery();
                    }
                }
            }
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_TrangPhucID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}